import Phaser, { Curves } from 'phaser';

export class Preloader extends Phaser.Scene {
	constructor() {
		super("Preloader");
	}

	//加载资源
	preload() {
		//加载一个图片
		this.load.image("hero", "../../static/logo.png");
		this.load.image("bg","../../static/game/bg/sky_background_mountains.png")
		this.load.spritesheet('king', '../../static/game/hero/hero_walk.png', { frameWidth: 64, frameHeight: 64 });
		this.load.spritesheet('attact', '../../static/game/hero/hero_thrust.png', { frameWidth: 192, frameHeight: 192 });
		this.load.spritesheet('skill', '../../static/game/hero/hero_slash.png', { frameWidth: 192, frameHeight: 192 });
	}
	//创建场景
	create() {
		//添加背景
		this.bg=this.add.tileSprite(0, 38, 800, 296, 'bg').setOrigin(0, 0)
		
		//示例向一个方向移动
		this.anims.create({
		            key: 'walk_up',
		            frames: this.anims.generateFrameNumbers('king',{start:0,end:7,first:0}),
		            frameRate: 16,		
		});
		this.anims.create({
		            key: 'walk_left',
		            frames: this.anims.generateFrameNumbers('king',{start:8,end:15,first:8}),
		            frameRate: 16,		
		});
		this.anims.create({
		            key: 'walk_down',
		            frames: this.anims.generateFrameNumbers('king',{start:16,end:23,first:16}),
		            frameRate: 16,		
		});
		this.anims.create({
		            key: 'walk_right',
		            frames: this.anims.generateFrameNumbers('king',{start:24,end:32,first:24}),
		            frameRate: 16,		
		});
		this.sprite = this.add.sprite(200, 300, 'king').setScale(1);
		this.sprite.play({ key: 'walk_up', repeat: -1 });
		
		const attactAnimation = this.anims.create({
		            key: 'attact',
		            frames: this.anims.generateFrameNumbers('attact',{start:7,end:11,first:7}),
		            frameRate: 7
		        });
		const sprite_attact = this.add.sprite(300, 300, 'attact').setScale(1);
		sprite_attact.play({ key: 'attact', repeat: -1 });
		
		const skillAnimation = this.anims.create({
		            key: 'skill',
		            frames: this.anims.generateFrameNumbers('skill',{start:0,end:4,first:0}),
		            frameRate: 7
		        });
		const sprite_skill = this.add.sprite(400, 300, 'skill').setScale(1);
		sprite_skill.play({ key: 'skill', repeat: -1 });
		
		
		this.add.sprite(100,100,'hero');
		// this.add
		// 	.text(200, 200, "飞机大战", {
		// 		fontFamily: "Arial",
		// 		fontSize: 60,
		// 		color: "#e3f2ed",
		// 		stroke: "#203c5b",
		// 		strokeThickness: 6,
		// 	})
		// 	.setOrigin(0.5);
		
		this.cursors = this.input.keyboard.createCursorKeys();
	}
	update(){
		//背景移动
		this.bg.tilePositionX -= 2;
		//实现人物移动
		if(this.cursors.up.isDown){
			this.sprite.play('walk_up', true);
			this.sprite.y-=0.1;
			
		}
		if(this.cursors.left.isDown){
			console.log('左边移动')
			this.sprite.play('walk_left', true);
			this.sprite.x-=0.1;
		}
		if(this.cursors.down.isDown){
			this.sprite.play('walk_down', true);
			this.sprite.y+=0.1;
		}
		if(this.cursors.right.isDown){
			this.sprite.play('walk_right', true);
			this.sprite.x+=0.1;
			
		}
		if(this.cursors.space.isDown){
			this.sprite.play('skill', true);
		}
		
	}
}